package com.bw.controller;

import com.bw.bean.Lei;
import com.bw.bean.Ying;
import com.bw.service.YingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: maven_ZK_xinshengil
 * @description
 * @author: Nick.Xin
 * @create: 2020-09-14 09:09
 **/
@Controller
public class YingController {

    @Resource
    private YingService service;

    //进入列表页
    @RequestMapping("list")
    public String findAllY(Model model,String aname,@RequestParam(defaultValue = "1") Integer pageNum){

        PageHelper.startPage(pageNum,3);
        Map<String,Object> map = new HashMap<>();
        map.put("aname",aname);
        List<Ying> list = service.findAllY(map);
        PageInfo plist = new PageInfo(list);
        model.addAttribute("plist",plist);
        model.addAttribute("aname",aname);
        return "list";
    }

    //多选框追加 所有类型
    @RequestMapping("duox")
    @ResponseBody
    public List<Lei> leis(){
        List<Lei> leis = service.leis();
        return leis;
    }

    //添加电影入口
    @RequestMapping("addYing")
    public String addYing(Ying ying,Integer[] ll){
        try {
            service.addYing(ying);
            for (Integer lid : ll) {
                service.addYlei(ying.getYid(),lid);
            }
            System.out.println("电影保存成功");
            return "addy";
        }catch (Exception e){
            e.printStackTrace();
            return "add";
        }
    }

    //删除入口
    @RequestMapping("dels")
    @ResponseBody
    public Boolean dels(Integer[] ids){
        try {
            service.delYing(ids);
            service.delYlei(ids);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    //回显多选框
    @RequestMapping("huixian")
    @ResponseBody
    public Ying findYingById(Integer yid){
        Ying list = service.findYingById(yid);
        return list;
    }


    //修改电影内容以及类型修改
    @RequestMapping("updYing")
    public String updYing(Ying ying,Integer[] ll){
        try {
            service.updYing(ying);
            Integer[] ids={ying.getYid()};
            service.delYlei(ids);
            for (Integer lid : ll) {
                service.addYlei(ying.getYid(),lid);
            }
            System.out.println("电影修改成功");
            return "redirect:list";
        }catch (Exception e){
            e.printStackTrace();
            return "update";
        }
    }
}

